package com.rppowell.pixie.swing;

import com.rppowell.pixie.models.MapProperties;
import java.awt.BorderLayout;

import javax.swing.JFrame;
import javax.swing.JOptionPane;

import com.rppowell.pixie.models.PixieProject;
import com.rppowell.pixie.models.Sprite;
import com.rppowell.pixie.models.SpriteSet;
import com.rppowell.pixie.models.SpriteSetProperties;
import com.rppowell.pixie.swing.importer.SpriteSetImporterProperties;
import com.rppowell.pixie.swing.spriteset.SpriteSetTabbedPane;
import java.util.logging.Level;
import java.util.logging.Logger;

public class PixieJFrame extends JFrame {

    /**
     *
     */
    private static final long serialVersionUID = 1L;
    private SpriteSetTabbedPane spriteSetTabbedPane;
    private PixieListener pixieListener;
    private PixieUIState pixieUIState;
    private PixieProject pixieProject;

    public PixieJFrame() {
        pixieProject = new PixieProject();
        
        pixieListener = PixieListener.INSTANCE;
        pixieListener.setPixieProject(pixieProject);
        pixieListener.setPixieJframe(this);

        pixieUIState = new PixieUIState();        
        PixieJMenuBar menuBar = new PixieJMenuBar(pixieListener);
        menuBar.setPixieUIState(pixieUIState);
        setJMenuBar(menuBar);

        setLayout(new BorderLayout());

        spriteSetTabbedPane = new SpriteSetTabbedPane();
        add(spriteSetTabbedPane, BorderLayout.CENTER);

        setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);        
        addWindowListener(pixieListener);
        setSize(640, 480);
        setVisible(true);
    }
   
    public void resetPixieUIState() {
        setPixieUIState(new PixieUIState());
    }
    
    public PixieUIState getPixieUIState() {
        return pixieUIState;
    }

    public void setPixieUIState(PixieUIState s) {
        pixieUIState = s;
        PixieJMenuBar menuBar = (PixieJMenuBar) getJMenuBar();
        menuBar.setPixieUIState(s);
        spriteSetTabbedPane.setPixieUIState(s);
    }
    
    public void doNewSpriteSet(SpriteSetProperties properties) {
        pixieProject.newSpriteSet(properties);
        updateFromPixieProject(pixieProject);        
    }

    public void doNewSprite() {
        SpriteSet set = getSelectedSpriteSet();
        if (set != null) {
            SpriteSetProperties p = set.getSpriteSetProperties();
            Sprite s = new Sprite(p.getSpriteWidth(), p.getSpriteHeight(), Sprite.TYPE_INT_ARGB);
            set.add(s);
        }
        updateFromPixieProject(pixieProject);        
    }
    
    public void doNewMap(MapProperties m) {
        pixieProject.newMap(m);
        updateFromPixieProject(pixieProject);
    }

    public void doSpriteSetImport(SpriteSetImporterProperties properties) {        
        Logger.getLogger(getClass().getName()).log(Level.FINEST, getClass().getName()+".doSpriteSetImport("+properties+")");
        pixieProject.importSpriteSet(properties);
        updateFromPixieProject(pixieProject);
    }

    public void updateFromPixieProject(PixieProject project) {
        pixieProject = project;
        spriteSetTabbedPane.setSpriteLibrary(project.getSpriteLibrary());
    }

    public void doWindowClosing() {
        if (false) {
            if (JOptionPane.showConfirmDialog(this, "Are you sure you want to exit?", "Exit?",
                    JOptionPane.YES_NO_OPTION,
                    JOptionPane.QUESTION_MESSAGE) == JOptionPane.YES_OPTION) {
                System.exit(0);
            }
        } else {
            System.exit(0);
        }
    }
    
    public SpriteSet getSelectedSpriteSet() {
        return spriteSetTabbedPane.getSelectedSpriteSet();        
    }
    public int getSelectedSpriteSetIndex() {
        return spriteSetTabbedPane.getSelectedIndex();
    }
    
    public void setSelectedSpriteInSpriteSet(SpriteSet spriteSet, Sprite sprite) {
        spriteSetTabbedPane.setSelectedSpriteInSpriteSet(spriteSet, sprite);
    }
}
